package com.xuecheng.media.api;

import com.alibaba.nacos.common.http.param.MediaType;
import com.xuecheng.media.model.dto.QueryMediaParamsDto;
import com.xuecheng.media.model.dto.UploadFileParamsDto;
import com.xuecheng.media.model.dto.UploadFileResultDto;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.service.MediaFilesService;
import com.xuecheng.model.PageParams;
import com.xuecheng.model.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
/**
 * @description 媒资文件管理接口
 * @author Mr.M
 * @date 2022/9/6 11:29
 * @version 1.0
 */
 @Api(value = "媒资文件管理接口",tags = "媒资文件管理接口")
 @RestController
public class MediaFilesController {


      @Autowired
      private MediaFilesService mediaFileService;


     @ApiOperation("媒资列表查询接口")
     @PostMapping("/files")
     public PageResult<MediaFiles> list(PageParams pageParams, @RequestBody QueryMediaParamsDto queryMediaParamsDto){
      Long companyId = 1232141425L;
      return mediaFileService.queryMediaFiels(companyId,pageParams,queryMediaParamsDto);
     }

     @ApiOperation("图片上传接口")
     @RequestMapping(value = "/upload/coursefile" , consumes = MediaType.MULTIPART_FORM_DATA)
     @ResponseBody
    public UploadFileResultDto FileUpload(@RequestPart("filedata")MultipartFile file ,
                                          //@RequestParam(value = "folder" , required = false) String folder,
                                          @RequestParam(value = "objectName" , required = false) String objectName) throws IOException {
         //机构ID
         long companyID = 1232141425;
         //获取文件路径,创建一个零时的文件
         File tempFile  = File.createTempFile("minio", ".temp");
         //使用transferTo方法将上传的文件保存到临时文件中
         file.transferTo(tempFile);
         //getAbsolutePath()方法获取临时文件的绝对路径
         String absolutePath = tempFile.getAbsolutePath();
         //构建一个UploadFileParamsDto来构建初始参数
         UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
         uploadFileParamsDto.setFileSize(file.getSize());
         uploadFileParamsDto.setFilename(file.getOriginalFilename());
         uploadFileParamsDto.setStatus("001001");
         UploadFileResultDto uploadFileResultDto = mediaFileService.uploadFile(companyID, uploadFileParamsDto, absolutePath , objectName);
         return  uploadFileResultDto;
     }




}
